package com.wsjc.web.controller;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.wsjc.web.config.JedisConfig;
import com.wsjc.web.entity.Operate;
import com.wsjc.web.entity.RedisInfoDetail;
import com.wsjc.web.service.RedisService;

/**
 * redis监控页面Controller
 * 
 * @author 作者: z77z
 * @date 创建时间：2017年3月29日 下午1:32:02
 */
@Controller
@RequestMapping(value="/redis/")
public class RedisController {
	
	@Autowired
	RedisService redisService;
	
	
	
	//跳转到摸个节点的监控页面
	@RequestMapping(value="showList")
	public String showList(Model model) {
		
		List<RedisInfoDetail> details = new ArrayList<>();
		List<String> showList = redisService.getNodesInfo();
		for(String address: showList){
			
			RedisInfoDetail detail = redisService.getRedisInfo(address);
			String mzl = null ;
            if(detail.getHit().equals("0") || detail.getMis().equals("0")){
            	mzl = "0.00";
			}else{
				int hit = Integer.valueOf(detail.getHit());
				int mis = Integer.valueOf(detail.getMis());
				mzl = getDouble(hit, hit+mis);
			}
			detail.setMzl(mzl);
			String status = redisService.getStatus(address);
			detail.setStatus(status);
			detail.setAddress(address);
			String useWayCode = JedisConfig.readConfig("use.way.code");
			detail.setType(useWayCode);
			details.add(detail);
		}
		/**
		 * 获取远程服务器配置
		 */
		model.addAttribute("nodes", details);
		return "redisList";
	}
	
	
	//跳转到摸个节点的监控页面
	@RequestMapping(value="monitor")
	public String redisMonitor(Model model, String address) {
		
		RedisInfoDetail detail = redisService.getRedisInfo(address);
		
		List<Operate> logList = redisService.getLogs(1000,address);
		long len = redisService.getLogLen(address);
		model.addAttribute("info", detail);
		model.addAttribute("logLen", len);
		model.addAttribute("logList", logList);
		model.addAttribute("address", address);
		
		return "redisMonitor";
	}
	//清空日志按钮
	@RequestMapping(value="logEmpty")
	@ResponseBody
	public String logEmpty(String address){
		return redisService.logEmpty(address);
	}
	
	//获取当前数据库中key的数量
	@RequestMapping(value="getKeysSize")
	@ResponseBody
	public String getKeysSize(String address){
		return JSON.toJSONString(redisService.getKeysSize(address));
	}
	
	//获取当前数据库内存使用大小情况
	@RequestMapping(value="getMemeryInfo")
	@ResponseBody
	public String getMemeryInfo(String address){
		return JSON.toJSONString(redisService.getMemeryInfo(address));
	}
	
	//获取当前数据库内存使用大小情况
	@RequestMapping(value="getCpuInfo")
	@ResponseBody
	public String getCpuInfo(String address){
		return JSON.toJSONString(redisService.getCpuInfo(address));
	}
	
	//获取每秒执行命令数
	@RequestMapping(value="getCmdInfo")
	@ResponseBody
	public String getCmdInfo(String address){
		return JSON.toJSONString(redisService.getCmdInfo(address));
	}
	
	
	//获取redis的info
	@RequestMapping(value="getInfo")
	@ResponseBody
	public String getInfo(String address){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("infos", redisService.getRedisInfo(address));
		return JSON.toJSONString(resultMap);
	}
	
	
	//获取redis的日志记录
	@RequestMapping(value="getLogs")
	@ResponseBody
	public String getLogs(String address){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("logs", redisService.getLogs(1000, address));
		resultMap.put("len", redisService.getLogLen(address));
		//获取redis的info
		return JSON.toJSONString(resultMap);
		
	}
	
	
	/** 转double **/
	public static String getDouble(int num,int sum) {
		NumberFormat nbf=NumberFormat.getInstance(); 
		nbf.setMinimumFractionDigits(2); 
		String c = nbf.format(Double.valueOf(num)/Double.valueOf(sum));
		return c;
	}		
	
}
